package com.ruoyi.pad.service.impl;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.qrKey.CacheQrKeyUtils;
import com.ruoyi.pad.domain.DeviceRegister;
import com.ruoyi.pad.domain.KyDeviceInfo;
import com.ruoyi.pad.domain.KyPadInfo;
import com.ruoyi.pad.domain.MediaUploadResp;
import com.ruoyi.pad.service.IDeskAppService;
import com.ruoyi.pad.service.IKyDeviceInfoService;
import com.ruoyi.pad.service.IKyPadInfoService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeskAppServiceImpl implements IDeskAppService {

    @Resource
    private IKyDeviceInfoService kyDeviceInfoService;
    @Resource
    private IKyPadInfoService kyPadInfoService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public MediaUploadResp uploadMedia(List<MultipartFile> files, String padSn, String appType) {
        MediaUploadResp mediaUploadResp = new MediaUploadResp();
        List<String> urls = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();
        for (MultipartFile file : files) {
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.uploadMedia(file);
            String url = Constants.RESOURCE_PREFIX_MEDIA + "/" + fileName;
            // 放入到redis队列中，异步生成缩略图
            redisTemplate.boundListOps("key-media").leftPush(fileName);
            urls.add(url);
            fileNames.add(fileName);
        }
        mediaUploadResp.setUrls(StringUtils.join(urls, ","));
        mediaUploadResp.setFileNames(StringUtils.join(fileNames, ","));
        return mediaUploadResp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean scanRegister(DeviceRegister register) {
        //绑定平板仪器信息
        KyDeviceInfo kyDeviceInfo = new KyDeviceInfo();
        //绑定用户信息到平板设备中
        LoginUser loginUser = CacheQrKeyUtils.getQrCache(register.getBindKey());
        kyDeviceInfo.setDevType(register.getDevType());
        kyDeviceInfo.setAppTypes(register.getAppTypes());
        kyDeviceInfo.setDevName(register.getDevName());
        kyDeviceInfo.setDevNum(register.getDevNum());
        kyDeviceInfo.setTeamUse(register.getTeamUse());
        kyDeviceInfo.setDeptId(loginUser.getDeptId());
        kyDeviceInfo.setUserId(loginUser.getUserId());
        kyDeviceInfo.setCreateBy(loginUser.getUsername());
        kyDeviceInfoService.insertKyDeviceInfo(kyDeviceInfo);
        //平板基本信息绑定
        KyPadInfo kyPadInfo = new KyPadInfo();
        kyPadInfo.setPadSn(register.getPadSn());
        kyPadInfo.setDevNum(register.getDevNum());
        kyPadInfo.setPadModel("");
        kyPadInfo.setPadImei(register.getPadImei());
        kyPadInfo.setPadVersion(register.getPadVersion());
        kyPadInfo.setPadApps(register.getPadApps());
        kyPadInfo.setDeptId(loginUser.getDeptId());
        kyPadInfo.setUserId(loginUser.getUserId());
        kyPadInfo.setCreateBy(loginUser.getUsername());
        kyPadInfoService.insertKyPadInfo(kyPadInfo);
        return true;
    }
}
